//
//  SNMyArtelAssociatorViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/17.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNAssociatorViewController.h"
#import "SNMyArtelAssociatorViewController.h"
#import "SNMyArtelAssociatorSectionHeaderView.h"
#import "SNCollectionViewLayoutAttributes.h"
#import "SNMyArtelAssociatorCell.h"
@interface SNMyArtelAssociatorViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,SNCollectionViewLayoutDelegate>
/** View */
@property (weak, nonatomic) UICollectionView * collectionView;
@end

@implementation SNMyArtelAssociatorViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的组织";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    [self addViews];
    [self setupUI];

}
- (UIColor *)setCollectionViewSectionBackGroundColor:(NSIndexPath *)indexPath{
    return [UIColor whiteColor];
}
- (void)addViews
{
    SNCollectionViewLayout * flowLayout = [[SNCollectionViewLayout alloc] init];
    flowLayout.delegate = self;
    /** 垂直方向 间距 */
    flowLayout.sn_minimumLineSpacing = 14;
    /** 横向方向 间距 */
    flowLayout.sn_minimumInteritemSpacing = 20;
    /** 内缩 内容 */
    flowLayout.sn_sectionInset = UIEdgeInsetsMake(13, 126, 13, 13);
    
    NSInteger colNum = 4;
    
    CGFloat w =  ([UIScreen mainScreen].bounds.size.width - flowLayout.sn_sectionInset.left - flowLayout.sn_sectionInset.right - flowLayout.sn_minimumInteritemSpacing * (colNum - 1)) / colNum;
    NSLog(@"w = %f",w);
    /** item大小 */
    flowLayout.sn_itemSize = CGSizeMake(w,66/667.0 * [UIScreen mainScreen].bounds.size.height);
    /** headerView大小 */
    flowLayout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 54+10);
//    /** footerView大小 */
//    flowLayout.footerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 69);
    /** 滚动方向 */
    // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:flowLayout];
    
    self.collectionView = collectionView;
    /** 分页处理 */
    // collectionView.pagingEnabled = YES;
    
    collectionView.backgroundColor = HexRGB(0xf5f5f5);
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"SNMyArtelAssociatorCell" bundle:nil] forCellWithReuseIdentifier:@"SNMyArtelAssociatorCell"];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[SNMyArtelAssociatorSectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SNMyArtelAssociatorSectionHeaderView"];
//    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableViewFooter"];
}
- (void)setupUI
{
    
}
#pragma mark - <UICollectionViewDataSource>
/** section 数量 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
/** item 数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSInteger count = 0;
//    switch (section) {
//        case 0:
//            count = 10;
//            break;
//        case 1:
//            count = 33;
//            break;
//        default:
//            break;
//    }
    return self.memberlist.count;
}
/** UICollectionViewCell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SNMyArtelAssociatorCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SNMyArtelAssociatorCell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor orangeColor];
    
    cell.model = self.memberlist[indexPath.item];
    
    return cell;
}
/** UICollectionReusableView */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        //此处是headerView
        SNMyArtelAssociatorSectionHeaderView *  headerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"SNMyArtelAssociatorSectionHeaderView" forIndexPath:indexPath];
        headerReusableView.backgroundColor = [UIColor whiteColor];
#warning 坑爹的服务器...
        headerReusableView.countLabel.text = [NSString stringWithFormat:@"%@个",self.memberqty];
//        headerReusableView.layer.borderColor = [UIColor blackColor].CGColor;
//        headerReusableView.layer.borderWidth = 1.0;
        return headerReusableView;
    }else if(kind == UICollectionElementKindSectionFooter){
        //此处是footerView
        UICollectionReusableView *  footerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableViewFooter" forIndexPath:indexPath];
        footerReusableView.backgroundColor = [UIColor greenColor];
        footerReusableView.layer.borderColor = [UIColor blackColor].CGColor;
        footerReusableView.layer.borderWidth = 1.0;
        return footerReusableView;
    }else{
        return nil;
    }
}
#pragma mark - <UICollectionViewDelegate>
// 选中操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);

    if (kUserInfo.userType.integerValue != 1) {
        
        SNAssociatorViewController * vc = [SNAssociatorViewController new];
        vc.USERID = [self.memberlist[indexPath.item] userId];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
// 取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
}
@end
